package vn.cuhanh.models;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vn.cuhanh.NewsDetailActivity;
import vn.cuhanh.R;
import vn.cuhanh.bean.RssItem;
import vn.cuhanh.utils.HelperFunc;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class Model_News {

	public static ArrayList<RssItem> parseRssFeed(String jsonString) {
		int lenght = 0;
		ArrayList<RssItem> itemlist = new ArrayList<RssItem>();
		if(jsonString == null) {
			return itemlist;
		}
		try{
			JSONObject jsonObject = new JSONObject(jsonString);
			int result = jsonObject.getInt("result");
			if(result != 1) {
				return null;
			}
			JSONArray rssList = jsonObject.getJSONArray("data");
			lenght = rssList.length();
			for(int i = 0; i < lenght; i++) {
				JSONObject item = rssList.getJSONObject(i);
				itemlist.add(new RssItem(item.getInt("id"), item.getString("title"), item.getString("date"), null, null, item.getString("link")));
			}
		} catch(JSONException ex) {
			HelperFunc.readStackTrace(ex);
		}
		return itemlist;
	}
	
	public static ArrayList<RssItem> retrieveRSSFeed(Context ctx, int productId, int categoryId, int page) {
		try {
			ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("productId", String.valueOf(productId)));
			params.add(new BasicNameValuePair("categoryId", String.valueOf(categoryId)));
			params.add(new BasicNameValuePair("page", String.valueOf(page)));
			Log.d("tab news", ctx.getResources().getString(R.string.getRssUrl));
			String response = HelperFunc.getDataFromSv(ctx.getResources().getString(R.string.getRssUrl), params);
			return parseRssFeed(response);
		} catch (Exception e) {
			HelperFunc.readStackTrace(e);
		}
		return null;
	}
	
	public static RssItem getSingleRssItem(Context ctx, int newsId) {
		try {
			ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("newsId", String.valueOf(newsId)));
			Log.d("tab news", ctx.getResources().getString(R.string.getRssUrl));
			String response = HelperFunc.getDataFromSv(ctx.getResources().getString(R.string.getSingleRssItemUrl), params);
			if(response == null) {
				return null;
			}
			try{
				JSONObject jsonObject = new JSONObject(response);
				int result = jsonObject.getInt("result");
				if(result != 1) {
					return new RssItem(0, null, null, null, null, null);
				}
				JSONObject data = jsonObject.getJSONObject("data");
				return new RssItem(data.getInt("id"), data.getString("title"), data.getString("date"), data.getString("description"), data.getString("content"), data.getString("link"));
			} catch(JSONException ex) {
				HelperFunc.readStackTrace(ex);
			}
		} catch (Exception e) {
			HelperFunc.readStackTrace(e);
		}
		return null;
	}
	
	public static void showNewsContent(final Context ctx, final int newsId) {
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				RssItem item = getSingleRssItem(ctx, newsId);
				if(item == null) {
					HelperFunc.showAlertDialogOnUiThread(ctx, null, ctx.getResources().getString(R.string.no_network_2));
					return;
				}
				String content = item.getContent(); 
				if(content != null && !content.trim().isEmpty()) {
					Intent intent = new Intent(ctx, NewsDetailActivity.class);
					intent.putExtra("type", NewsDetailActivity.TYPE_RAW_CONTENT);
					intent.putExtra("content", content);
					ctx.startActivity(intent);
					return;
				}
				String link = item.getLink();
				if(link != null && !link.trim().isEmpty()) {
					Intent intent = new Intent(ctx, NewsDetailActivity.class);
					intent.putExtra("type", NewsDetailActivity.TYPE_URL);
					intent.putExtra("content", link);
					ctx.startActivity(intent);
					return;
				}
				HelperFunc.showAlertDialogOnUiThread(ctx, null, ctx.getResources().getString(R.string.rssItemNotValid));
			}
		});
		thread.start();
	}
}
